package base

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/base"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/base/role"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
	"strconv"
)

type RoleController struct {
	controller.BaseController
	roleService role.RoleServiceIface
}

func NewRoleController(roleService role.RoleServiceIface) *RoleController {
	return &RoleController{
		roleService: roleService,
	}
}

// RegisterRouter
// 注册路由
func (c *RoleController) RegisterRouter(g *gin.RouterGroup) {
	// 新增/修改角色
	g.POST("/role/save", c.RoleSave)
	// 删除角色
	g.POST("/role/delete", c.RoleDelete)
	// 根据角色获取权限列表
	g.GET("/role/permission_list", c.RolePermissionList)
	// 角色列表
	g.GET("/role/list", c.RoleList)
	// 增加权限到角色
	g.POST("/role/add_permission", c.RoleAddPermission)
}

func (c *RoleController) RoleSave(ctx *gin.Context) {
	param := &base.RoleItem{}
	if err := ctx.ShouldBind(param); err != nil {
		stark.Logger.Infof(ctx, "RoleSave-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	if param.Name == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "角色名称为空")
		return
	}

	roleId, err := c.roleService.RoleSave(ctx, param)
	if err != nil {
		stark.Logger.Infof(ctx, "RoleSave-err: %+v, param: %+v", err, param)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"id": roleId,
	})
}

func (c *RoleController) RoleDelete(ctx *gin.Context) {
	param := &base.RoleParam{}
	if err := ctx.ShouldBind(param); err != nil {
		stark.Logger.Infof(ctx, "RoleDelete-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	if param.RoleId == 0 {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "角色编号为空")
		return
	}

	roleId, err := c.roleService.RoleDelete(ctx, param)
	if err != nil {
		stark.Logger.Infof(ctx, "RoleDelete-err: %+v, param: %+v", err, param)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"id": roleId,
	})
}

func (c *RoleController) RolePermissionList(ctx *gin.Context) {
	roleIdStr := ctx.Query("role_id")
	roleId, _ := strconv.Atoi(roleIdStr)
	if roleId == 0 {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "角色编号为空")
		return
	}

	param := &base.RoleParam{RoleId: int64(roleId)}
	list, err := c.roleService.PermissionList(ctx, param)
	if err != nil {
		stark.Logger.Infof(ctx, "RolePermissionList-err: %+v, param: %+v", err, param)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"list": list,
	})
}

func (c *RoleController) RoleList(ctx *gin.Context) {
	list, err := c.roleService.RoleList(ctx)
	if err != nil {
		stark.Logger.Infof(ctx, "RoleList-err: %+v", err)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, map[string]interface{}{
		"list": list,
	})
}

func (c *RoleController) RoleAddPermission(ctx *gin.Context) {
	param := &base.AddPermission{}
	if err := ctx.ShouldBind(param); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	if param.RoleId == 0 {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "缺少参数")
		return
	}

	if len(param.PerIds) == 0 {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "角色关联权限不能为空")
		return
	}

	err := c.roleService.AddPermissionToRole(ctx, param.RoleId, param.PerIds)
	if err != nil {
		stark.Logger.Infof(ctx, "RoleAddPermission-info, param: %+v, err: %+v", param, err)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	c.Success(ctx, nil)
}
